package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class StartPanel extends JPanel{
    private String[] menus = new String[]{"游戏开始","游戏设置","游戏帮助","游戏退出"};
    private int menu_x = 350;
    private int[] menu_y = new int[]{150,250,350,450};
    private int falg;
    StartFrame frame;
    BufferedImage bj = ImageUtil.getImage("/Images/bj.jpg");
    public StartPanel(StartFrame frame){
        this.frame = frame;
        KeyListener();
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bj,0,0,null);
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        for (int i = 0; i < menus.length; i++) {
            int x = menu_x;
            int y = menu_y[i];
            if (i == falg){
                g.setColor(Color.red);
            }else {
                g.setColor(Color.BLACK);
            }
            g.drawString(menus[i],x,y);
        }
    }
    //键盘监听
    public void KeyListener(){
        KeyAdapter adapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int KeyCode = e.getKeyCode();
                if (KeyCode == KeyEvent.VK_UP){
                    falg -= 1;
                    if (falg < 0){
                        falg = 3;
                    }
                    repaint();
                }else if (KeyCode == KeyEvent.VK_DOWN){
                    falg += 1;
                    if (falg > 3){
                        falg = 0;
                    }
                    repaint();
                }else if (KeyCode == KeyEvent.VK_ENTER){
                    switch (falg){
                        case 0:
                            new GameFrame();
                            frame.dispose();//关闭当前窗口
                            System.out.println("游戏开始");
                            break;
                        case 1:
                            System.out.println("游戏设置");
                            new SetFrame(frame);
                            frame.dispose();
                            break;
                        case 2:
                            System.out.println("游戏帮助");
                            break;
                        case 3:
                            System.out.println("游戏退出");
                            System.exit(0);
                            break;
                    }
                }
            }
        };
        frame.addKeyListener(adapter);
    }
}
